package com.xunyin.nettyV.client;

import com.alibaba.fastjson.JSONObject;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2023-3-16
 * mqtt客户端发布回调
 */
public class PushCallback implements MqttCallback {

    private String threadId;

    public PushCallback(String threadId) {
        this.threadId = threadId;
    }

    public void connectionLost(Throwable cause) {
        // 连接丢失后，一般在这里面进行重连
        System.out.println("连接断开，可以做重连");
    }

    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());
        try {
            System.out.println("deliveryComplete-------Message--" + JSONObject.toJSONString(token.getMessage()));
        } catch (MqttException e) {
            e.printStackTrace();
            System.out.println("deliveryComplete-------error--");
        }
    }

    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String msg = new String(message.getPayload());
        System.out.println("-------messageArrived-------" + threadId + " " + msg);
    }


}
